package com.pair.micro.common.utils;

import cn.hutool.core.net.NetUtil;
import cn.hutool.system.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.Amount;
import com.wechat.pay.java.service.payments.app.model.PrepayRequest;
import com.wechat.pay.java.service.payments.app.model.PrepayResponse;


import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;

/**
 * 系统工具
 */
@Slf4j
public class MicroSystemUtil {

  /**
   * @name: getProjectRootPath
   * @description 获取项目根路径（容器为Tomcat）
   * @author xubz
   * @date 2023-09-011 14:37
   * @return: java.lang.String
   */
  public static String getProjectRootPath(Class cs) {
    String rootPath = "";

    if (MicroOsUtil.isWindows()) {
      rootPath =
          cs.getResource("/").getPath().replace("WEB-INF/classes/", "").replaceFirst("/", "");
    } else {
      rootPath = cs.getResource("/").getPath().replace("WEB-INF/classes/", "");
    }

    return rootPath;
  }

  /**
   * @name: availableProcessors
   * @description 查询当前服务器可用的处理器数量
   * @author xubz
   * @date 2023-09-011 14:37
   * @return: int
   */
  public static int availableProcessors() {
    return SystemUtil.getRuntimeInfo().getRuntime().availableProcessors();
  }

  /**
   * @name: getLocalIpv4
   * @description 获取本机ipv4地址
   * @author xubz
   * @date 09/18/23 1:59 PM
   * @return: java.lang.String
   */
  public static String getLocalIpv4() {
    LinkedHashSet<String> localIpv4s = NetUtil.localIpv4s();
    return localIpv4s.stream()
        .filter(localIpv4 -> !localIpv4.equals(NetUtil.LOCAL_IP))
        .findFirst()
        .orElse(NetUtil.LOCAL_IP);
  }

  /** 商户号 */
  public static String merchantId = "1669151310";
  /** 商户API私钥路径 */
  public static String privateKeyPath = "F:/hexinhangRemodel/hxh-front/src/main/resources/apiclient_key.pem";
  /** 商户证书序列号 */
  public static String merchantSerialNumber = "4644C738FCF21A944AC05BAFB428C1E9C2B8AF0F";
  /** 商户APIV3密钥 */
  public static String apiV3Key = "Hxhh7UhoQlVtt1C2Z4KN3yYlm3JTR2qi";


  public static void main(String[] args) throws IOException {
    InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("apiclient_key.pem");
    // 使用自动更新平台证书的RSA配置
    // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
    Config config =
            new RSAAutoCertificateConfig.Builder()
                    .merchantId(merchantId)
                    .privateKeyFromPath(privateKeyPath)
                    .merchantSerialNumber(merchantSerialNumber)
                    .apiV3Key(apiV3Key)
                    .build();
    // 构建service
    AppService service = new AppService.Builder().config(config).build();
    // request.setXxx(val)设置所需参数，具体参数可见Request定义
    PrepayRequest request = new PrepayRequest();
    Amount amount = new Amount();
    amount.setTotal(100);
    request.setAmount(amount);
    request.setAppid("wx6a7b7edbe55b1b77");
    request.setMchid("1669151310");
    request.setDescription("测试商品标题");
    request.setNotifyUrl("https://notify_url");
    request.setOutTradeNo("202403151768491181787746304");
    // 调用下单方法，得到应答
    PrepayResponse response = service.prepay(request);
    // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
    System.out.println(response.getPrepayId());
  }
}
